package com.dianping.cat.consumer.dependency.model.transform;

import com.dianping.cat.consumer.dependency.model.entity.Dependency;
import com.dianping.cat.consumer.dependency.model.entity.DependencyReport;
import com.dianping.cat.consumer.dependency.model.entity.Index;
import com.dianping.cat.consumer.dependency.model.entity.Segment;
import java.text.DecimalFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import org.xml.sax.Attributes;











public class DefaultSaxMaker
  extends Object
  implements IMaker<Attributes>
{
  public Dependency buildDependency(Attributes attributes) {
/*  28 */     String type = attributes.getValue("type");
    String target = attributes.getValue("target");
/*  30 */     String totalCount = attributes.getValue("total-count");
    String avg = attributes.getValue("avg");
    String errorCount = attributes.getValue("error-count");
/*  33 */     String key = attributes.getValue("key");
/*  34 */     String sum = attributes.getValue("sum");
/*  35 */     Dependency dependency = new Dependency(key);

/*  37 */     if (type != null) {
/*  38 */       dependency.setType(type);
    }

/*  41 */     if (target != null) {
/*  42 */       dependency.setTarget(target);
    }

/*  45 */     if (totalCount != null) {
/*  46 */       dependency.setTotalCount(((Long)convert(Long.class, totalCount, Long.valueOf(0L))).longValue());
    }

/*  49 */     if (avg != null) {
/*  50 */       dependency.setAvg(toNumber(avg, "0.00", Integer.valueOf(0)).doubleValue());
    }

/*  53 */     if (errorCount != null) {
/*  54 */       dependency.setErrorCount(((Long)convert(Long.class, errorCount, Long.valueOf(0L))).longValue());
    }

/*  57 */     if (sum != null) {
/*  58 */       dependency.setSum(toNumber(sum, "0.00", Integer.valueOf(0)).doubleValue());
    }

/*  61 */     return dependency;
  }


  public DependencyReport buildDependencyReport(Attributes attributes) {
/*  66 */     String domain = attributes.getValue("domain");
/*  67 */     String startTime = attributes.getValue("startTime");
/*  68 */     String endTime = attributes.getValue("endTime");
/*  69 */     DependencyReport dependencyReport = new DependencyReport(domain);

/*  71 */     if (startTime != null) {
/*  72 */       dependencyReport.setStartTime(toDate(startTime, "yyyy-MM-dd HH:mm:ss", null));
    }

/*  75 */     if (endTime != null) {
/*  76 */       dependencyReport.setEndTime(toDate(endTime, "yyyy-MM-dd HH:mm:ss", null));
    }

/*  79 */     return dependencyReport;
  }



/*  84 */   public String buildDomainName(Attributes attributes) { throw new UnsupportedOperationException(); }



  public Index buildIndex(Attributes attributes) {
/*  89 */     String name = attributes.getValue("name");
/*  90 */     String totalCount = attributes.getValue("total-count");
/*  91 */     String errorCount = attributes.getValue("error-count");
/*  92 */     String sum = attributes.getValue("sum");
/*  93 */     String avg = attributes.getValue("avg");
/*  94 */     Index index = new Index(name);

/*  96 */     if (totalCount != null) {
/*  97 */       index.setTotalCount(((Long)convert(Long.class, totalCount, Long.valueOf(0L))).longValue());
    }

/* 100 */     if (errorCount != null) {
/* 101 */       index.setErrorCount(((Long)convert(Long.class, errorCount, Long.valueOf(0L))).longValue());
    }

/* 104 */     if (sum != null) {
/* 105 */       index.setSum(toNumber(sum, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 108 */     if (avg != null) {
/* 109 */       index.setAvg(toNumber(avg, "0.00", Integer.valueOf(0)).doubleValue());
    }

/* 112 */     return index;
  }


  public Segment buildSegment(Attributes attributes) {
/* 117 */     String id = attributes.getValue("id");
/* 118 */     String exceptionCount = attributes.getValue("exception-count");
/* 119 */     Segment segment = new Segment((id == null) ? null : (Integer)convert(Integer.class, id, null));

/* 121 */     if (exceptionCount != null) {
/* 122 */       segment.setExceptionCount((Integer)convert(Integer.class, exceptionCount, null));
    }

/* 125 */     return segment;
  }


  protected <T> T convert(Class<T> type, String value, T defaultValue) {
/* 130 */     if (value == null || value.length() == 0) {
/* 131 */       return defaultValue;
    }

/* 134 */     if (type == Boolean.class || type == boolean.class)
/* 135 */       return (T)Boolean.valueOf(value); 
/* 136 */     if (type == Integer.class || type == int.class)
/* 137 */       return (T)Integer.valueOf(value); 
/* 138 */     if (type == Long.class || type == long.class)
/* 139 */       return (T)Long.valueOf(value); 
/* 140 */     if (type == Short.class || type == short.class)
/* 141 */       return (T)Short.valueOf(value); 
/* 142 */     if (type == Float.class || type == float.class)
/* 143 */       return (T)Float.valueOf(value); 
/* 144 */     if (type == Double.class || type == double.class)
/* 145 */       return (T)Double.valueOf(value); 
/* 146 */     if (type == Byte.class || type == byte.class)
/* 147 */       return (T)Byte.valueOf(value); 
/* 148 */     if (type == Character.class || type == char.class) {
/* 149 */       return (T)Character.valueOf(value.charAt(0));
    }
/* 151 */     return (T)value;
  }


  protected Date toDate(String str, String format, Date defaultValue) {
/* 156 */     if (str == null || str.length() == 0) {
/* 157 */       return defaultValue;
    }

    try {
/* 161 */       return (new SimpleDateFormat(format)).parse(str);
/* 162 */     } catch (ParseException e) {
/* 163 */       throw new RuntimeException(String.format("Unable to parse date(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }

  protected Number toNumber(String str, String format, Number defaultValue) {
/* 168 */     if (str == null || str.length() == 0) {
/* 169 */       return defaultValue;
    }

    try {
/* 173 */       return (new DecimalFormat(format)).parse(str);
/* 174 */     } catch (ParseException e) {
/* 175 */       throw new RuntimeException(String.format("Unable to parse number(%s) in format(%s)!", new Object[] { str, format }), e);
    }
  }
}


/* Location:              /Users/hanfei/Documents/apache-tomcat-9.0.24/webapps/cat/WEB-INF/lib/cat-consumer-3.0.0.jar!/com/dianping/cat/consumer/dependency/model/transform/DefaultSaxMaker.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       1.0.7
 */